<?php

namespace app\api\controller;

use app\admin\model\ConfigValueModel;
use app\admin\model\PublishCommentReport;
use app\common\controller\Api;
use app\common\model\PublishComment;


class Report extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @var PublishCommentReport
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new PublishCommentReport();
    }



    public function option_list()
    {
        $data = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_REPORT);
        $this->success('success', $data);
    }

    public function add()
    {
        $params = $this->request->post();
        if (empty($params['type'])) {
            $this->error('类型不能为空');
        }

        if (empty($params['comment_id'])) {
            $this->error('举报的评论ID不能为空');
        }

        $comment = PublishComment::get($params['comment_id']);
        if (!$comment) {
            $this->error('举报的评论不存在');
        }

        $option_list = (new ConfigValueModel())->getOptionList(ConfigValueModel::TYPE_REPORT);
        if (!array_key_exists($params['type'], $option_list)) {
            $this->error('类型错误');
        }

        if (empty($params['content'])) {
            $this->error('内容不能为空');
        }

        $create = $this->model->create([
            'user_id' => $this->auth->id,
            'comment_id' => $params['comment_id'],
            'type' => $params['type'],
            'content' => $params['content'],
            //'images' => empty($params['images']) ? null : json_encode($params['images']),
            'report_name' => $params['report_name'] ?? '',
            'report_mobile' => $params['report_mobile'] ?? '',
        ]);

        if ($create->id) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
}
